package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.dao.SysDataDowMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysDataDow;
import com.bysj.sausys.service.ISysDataDowService;
import com.bysj.sausys.util.FileUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源下载Service实现
 *
 * @author WangZK
 */
@Service("sysDataDowService")
public class SysDataDowService implements ISysDataDowService {


    @Resource
    private SysDataDowMapper sysDataDowMapper;

    /**
     * 新增资料
     *
     * @param sysDataDow
     * @param username
     */
    @Override
    public void save(SysDataDow sysDataDow, String username) {
        //进行数据校验
        if (checkDataDowExist(sysDataDow.getId(), sysDataDow.getName())) {
            throw new ParamException("资料已存在，保存失败");
        }

        sysDataDow.setInsertMan(username);
        sysDataDow.setInsertTime(new Date());
        sysDataDowMapper.insertSelective(sysDataDow);
    }

    /**
     * 判断资料是否已存在
     *
     * @param id
     * @param name
     * @return
     */
    private boolean checkDataDowExist(Integer id, String name) {
        return sysDataDowMapper.countByName(id, name) > 0;
    }

    /**
     * 更新资料
     *
     * @param sysDataDow
     * @param username
     */
    @Override
    public void update(SysDataDow sysDataDow, String username) {
        SysDataDow before = sysDataDowMapper.selectByPrimaryKey(sysDataDow.getId());
        if (before == null) {
            throw new ParamException("待更新的资料信息不存在");
        }
        if (checkDataDowExist(sysDataDow.getId(), sysDataDow.getName())) {
            throw new ParamException("资料已存在，更新失败");
        }

        SysDataDow after = new SysDataDow();

        after.setId(sysDataDow.getId());
        after.setName(sysDataDow.getName());
        after.setPath(sysDataDow.getPath());
        after.setRemarks(sysDataDow.getRemarks());
        after.setStatus(sysDataDow.getStatus());
        after.setLastupdMan(username);
        after.setLastupdTime(new Date());

        sysDataDowMapper.updateByPrimaryKeySelective(after);
    }

    /**
     * 分页获取资料
     *
     * @param path
     * @param keyword
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getPageByKeyword(String path, String keyword, PageQuery pageQuery) {
        int count = sysDataDowMapper.countByKeyword(keyword);
        if (count > 0) {
            List<SysDataDow> sysDataDowList = sysDataDowMapper.getPageByKeyword(keyword, pageQuery);
            PageResult<SysDataDow> result = new PageResult<>();
            result.setList(sysDataDowList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取资料
     *
     * @param id
     * @return
     */
    @Override
    public SysDataDow findById(Integer id) {
        return sysDataDowMapper.selectByPrimaryKey(id);
    }

    /**
     * 设置资料状态
     *
     * @param id
     * @param status
     * @param username
     */
    @Override
    public void setDataDowStatus(Integer id, int status, String username) {
        Map params = new HashMap(4);
        params.put("id", id);
        params.put("status", status);
        params.put("lastupdTime", new Date());
        params.put("lastupdMan", username);
        sysDataDowMapper.updateStatusByPrimaryKey(params);
    }

    /**
     * 删除资料
     *
     * @param id
     * @param path
     */
    @Override
    public void delete(Integer id, String path) {
        SysDataDow sysDataDow = sysDataDowMapper.selectByPrimaryKey(id);
        try {
            FileUtil.deleteFile(path + sysDataDow.getPath());
            sysDataDowMapper.deleteByPrimaryKey(id);
        } catch (IOException e) {
            throw new ParamException("文件不存在，删除失败");
        }
    }

    /**
     * 根据资源状态分页获取资源列表
     *
     * @param path
     * @param status
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getPageByStatus(String path, Integer status, PageQuery pageQuery) {
        int count = sysDataDowMapper.countByStatus(status);
        if (count > 0) {
            List<SysDataDow> sysDataDowList = sysDataDowMapper.getPageByStatus(status, pageQuery);
            PageResult<SysDataDow> result = new PageResult<>();
            result.setList(sysDataDowList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }
}
